class ScoreMgr {
  /**
   * @param scores 学生分数数组
   */
  constructor(scores) {
    // 分数个数装入数组
    this.scoreCnt = new Array(100).fill(0)
    for (const score of scores) {
      this.scoreCnt[score]++
    }

    // 统计分数前缀和
    this.preSum = new Array(100 + 1).fill(0)
    for (let i = 1; i < this.preSum.length; i++) {
      this.preSum[i] = this.preSum[i - 1] + this.scoreCnt[i - 1]
    }
  }

  /**
   * 查询分数范围scoreStart ~ scoreEnd的学生个数
   * @param scoreRange [scoreStart, scoreEnd]
   */
  queryStudentCnt(scoreRange) {
    if (Array.isArray(scoreRange) && scoreRange[0] <= scoreRange[1]) {
      const [scoreStart, scoreEnd] = scoreRange
      return this.preSum[scoreEnd + 1] - this.preSum[scoreStart]
    }

    throw Error('查询的分数区间错误')
  }
}

const mgr = new ScoreMgr([88, 22, 45, 98, 20])
console.log(
  mgr.queryStudentCnt([10, 50])
)
